package cn.xiaopengstack.domain.trade.rule.settlement;

import cn.xiaopengstack.domain.trade.rule.factory.SettlementRuleFilterFactory;
import cn.xiaopengstack.domain.trade.model.entity.SettlementRuleCommandEntity;
import cn.xiaopengstack.domain.trade.model.entity.SettlementRuleResponseEntity;
import cn.xiaopengstack.domain.trade.repository.ITradeRepository;
import cn.xiaopengstack.types.enums.ResponseCode;
import cn.xiaopengstack.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author jiangyangang
 */
@Component
@Slf4j
public class SCRuleFilter extends AbstractSettlementLogicLink<SettlementRuleCommandEntity, SettlementRuleFilterFactory.DynamicContext, SettlementRuleResponseEntity> {

    public SCRuleFilter(ITradeRepository tradeRepository) {
        super(tradeRepository);
    }

    @Override
    public SettlementRuleResponseEntity apply(SettlementRuleCommandEntity request, SettlementRuleFilterFactory.DynamicContext dynamic) throws Exception {
        log.info("结算规则流。黑名单节点开始");
        boolean scBlack = tradeRepository.isSCBlack(request.getSource(), request.getChannel());
        if (scBlack) {
            throw new AppException(ResponseCode.SETTLEMENT_FILTER_SC_VALIDATE_FAIL.getCode(), ResponseCode.SETTLEMENT_FILTER_SC_VALIDATE_FAIL.getInfo());
        }

        return next(request, dynamic);
    }

}
